package com.cloud.utils;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.UUID;

/**
 * Created by Administrator on 2017/11/16 0016.
 */
public class FileUtil {
    @Value("file.upload.rootPath")
    private String uploadRootPath;

    /**
     * 文件上传
     *
     * @param childPath  相对于根目录的子目录
     * @param uploadFile 要上传的文件
     * @return 上传成功之后的文件名
     * @throws Exception
     */
    public String upload(String childPath, MultipartFile uploadFile) throws Exception {
        File file = new File(uploadRootPath + File.separator + childPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        //原文件名
        String uploadFileName = uploadFile.getOriginalFilename();
        //后缀名
        String ext = uploadFileName.substring(uploadFileName.lastIndexOf("."));
        String dbFileName = "";
        if (isVedio(ext)) {
            dbFileName = "vedio-" + UUID.randomUUID() + "-" + DateUtil.getCurrentDateStr() + ext;
        } else if (isImage(ext)) {
            dbFileName = "image-" + UUID.randomUUID() + "-" + DateUtil.getCurrentDateStr() + ext;
        } else {
            dbFileName = "file-" + UUID.randomUUID() + "-" + DateUtil.getCurrentDateStr() + ext;
        }
        uploadFile.transferTo(new File(uploadRootPath + File.separator + childPath + File.separator + dbFileName));
        return dbFileName;
    }

    /**
     * 文件下载
     *
     * @param childPath        相对于根目录的子目录
     * @param dbFileName       源文件名，即数据库中的文件名
     * @param downLoadFileName 下载时显示的文件名
     * @return
     * @throws Exception
     */
    public ResponseEntity<byte[]> downLoad(String childPath, String dbFileName, String downLoadFileName) throws Exception {
        HttpHeaders headers = new HttpHeaders();
        File file = new File(uploadRootPath + File.separator + childPath + File.separator + dbFileName);
        if (!file.exists()) {
            throw new FileNotFoundException("下载文件不存在");
        }
        downLoadFileName = new String(downLoadFileName.getBytes("utf-8"), "ISO8859-1");
        headers.setContentDispositionFormData("attachment", downLoadFileName);
        //application/octet-stream ： 二进制流数据。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
    }

    /**
     * 是否为视频文件
     *
     * @param ext
     * @return
     */
    private boolean isVedio(String ext) {
        String[] vedioArray = {".wmv", ".avi", ".rmvb", ".rm", ".mp4", ".3gp", ".mkv"};
        return Arrays.asList(vedioArray).contains(ext);
    }

    /**
     * 是否为图片
     *
     * @param ext
     * @return
     */
    private boolean isImage(String ext) {
        String[] imageArray = {".jpg", ".gif", ".png", ".bmp"};
        return Arrays.asList(imageArray).contains(ext);
    }
}
